<?php
include("general_include.php");
include("class/image_class.php");

include "checklogin.php";
include "top.php";
//include "left.php";

//Image object creation
$create_type="GDThumbnail";
$image_object=new ImageClass($create_type,100,1);

if(isset($_POST['submitForm']))
{
	  $Formval = $_POST; 
	  $name = trim($_REQUEST['name']);
	  $contact_no = trim($_POST['contact_no']);
	  $city = trim($_POST['cityid']);
	  $city_name = trim($_POST['city']);
	  $skype_id = trim($_POST['skype_id']);
	  $paypal_email_id = trim($_POST['paypal_email_id']);
	  $user_about = trim($_POST['user_about']);
	  $description = trim($_POST['description']);
	  $date_of_birth = $_POST['date_of_birth'];
	  $lang_count=count($_POST['language']);
	  $description = $_POST['description'];
	  $availability_type1 = trim($_POST['availability_type1']);
	  $availability_type2 = trim($_POST['availability_type2']);
	  $availability_type = $_POST['availability_type'];
	  $availability_city = trim($_POST['availability_city']);
	  $acity = $_POST['acityid'];
	  $user_type = $_POST['user_type'];
	  for($l=0; $l<$lang_count; $l++)
	  {
	    $language_id.= $language[$l].',';
	  }
	  $language_ids = substr($language_id,0,-1);
	  if($_FILES['upload']['name']!='')
	    {
		     $uploaddir = "uploaded/user_images/";
			 $img_name = rand(1,1000).str_replace(" ","_",$_FILES['upload']['name']);
			 $uploadfile = $uploaddir.$img_name;
			 move_uploaded_file($_FILES['upload']['tmp_name'],"uploaded/user_images/".$img_name);
			  
			 @unlink('uploaded/user_images/'.$_POST['oldphoto']);
			 @unlink('uploaded/user_images/thumbs/big_'.$_POST['oldphoto']);
			 @unlink('uploaded/user_images/thumbs/small_'.$_POST['oldphoto']);
			 @unlink('uploaded/user_images/thumbs/mid_'.$_POST['oldphoto']);
			 @unlink('uploaded/user_images/thumbs/'.$_POST['oldphoto']);
			 
			 $smallimagename = "thumbs/small_" . $img_name;
			 $mediumimagename = "thumbs/mid_" . $img_name;
		     $bigimagename = "thumbs/big_" . $img_name;
		     $destimagename = "thumbs/" . $img_name;
			 
			 $image_object->SetImagePath($uploaddir);
			 $image_object->create_gdthumbnail($uploadfile,$destimagename,37,5,37); // creating tiny
			 $image_object->create_gdthumbnail($uploadfile,$smallimagename,57,5,50); // creating small
			 $image_object->create_gdthumbnail($uploadfile,$mediumimagename,105,5,92);
			 $image_object->create_gdthumbnail($uploadfile,$bigimagename,198,5,172); // creaging big
			 
			 $sqlUser = "SELECT * FROM ".TABLEPREFIX."_user where user_id='".$_SESSION['user_id']."'";
			 $Formval = $UserManagerObjAjax->GetRecords("Row",$sqlUser);
			 // updating avatar to forum user //
			 //mysql_query("UPDATE forum_users SET user_avatar='".SITE_URL."/".$uploaddir.$bigimagename."', user_avatar_type='2', user_avatar_width='57', 
			 //user_avatar_height='50' WHERE user_email='".$Formval['email']."'");
			 ////			 
		}
		else
		    $img_name = $_POST['oldphoto'];
	       
		   if($availability_type1!='' && $availability_type2!='')
			  $availability_type = $availability_type1.','.$availability_type2;
			if($availability_type1!='' && $availability_type2=='')
			{   
			  $availability_type = $availability_type1;
			  $acity='';
			  $availability_country='';
			}  
			if($availability_type2!='' && $availability_type1=='')   
			  $availability_type = $availability_type2;  
			  
			  if($city!= '') 
			  {
			   $sqlCity = "SELECT * FROM ".TABLEPREFIX."_cities WHERE city_id='$city'";
			   $rowCity = $UserManagerObjAjax->GetRecords("Row",$sqlCity);
			   $country = $rowCity['country_id'];
			 }
			  if($acity!= '' && $availability_type2!='') 
			 {
			   $sqlCity = "SELECT * FROM ".TABLEPREFIX."_cities WHERE city_id='$acity'";
			   $rowCity = $UserManagerObjAjax->GetRecords("Row",$sqlCity);
			   $availability_country = $rowCity['country_id']; 
			 }     
	
	 
	  $UpdateUser = "UPDATE ".TABLEPREFIX."_user set name='$name',contact_no='$contact_no',city='$city',country='$country',paypal_email_id='$paypal_email_id', 
	  date_of_birth='$date_of_birth',skype_id='$skype_id',date_edited='".date('Y-m-d')."',language_ids='$language_ids',photo='$img_name',availability_type='$availability_type',
	  availability_city='$acity',availability_country='$availability_country',description='".addslashes($description)."' WHERE user_id='".$_SESSION['user_id']."'";
	  $UserManagerObjAjax->Execute($UpdateUser);
	  
	/*  if($user_type!='') {
	  $Updatetype = "INSERT INTO ".TABLEPREFIX."_user_type_user_relation set user_type_id='$user_type',user_id='".$_SESSION['user_id']."',date_added='".date('Y-m-d')."'";
	  $UserManagerObjAjax->Execute($Updatetype);
	  }
	  
      if(!mysql_error())
	  {
	    if($user_type ==1)
		{
		   echo "<script>window.location.href='coach_game_update.php?profile=Y'</script>";
		   exit;
		}
		else if($user_type== 3)
		{
		   echo "<script>window.location.href='training_partner_game_update.php?profile=Y'</script>";
		   exit;
		
		}
		else
		{
		  echo "<script>window.location.href='profile.php?messg=2'</script>";
		  exit;
		}  
	  }*/
	  if(!mysql_error())
	  {
          echo "<script>window.location.href='profile.php?messg=2'</script>";
		  exit;
	  }	  
}

$sqlUser = "SELECT * FROM ".TABLEPREFIX."_user where user_id='".$_SESSION['user_id']."'";
$Formval = $UserManagerObjAjax->GetRecords("Row",$sqlUser);

$Formval['user_about'] = stripslashes($Formval['user_about']);
$Formval['description'] = stripslashes($Formval['description']);
$avail = explode(',',$Formval["availability_type"]);
if($avail[1]=='' && $avail[0]=='L')
 $Formval["availability_type2"]= $avail[0];
if($avail[1]=='' && $avail[0]=='O') 
 $Formval["availability_type1"]= $avail[0];
if($avail[1]!='')
{
 $Formval["availability_type1"]= $avail[0];
 $Formval["availability_type2"]= $avail[1];
} 
$Formval["char_left"] = 300 - strlen($Formval["description"]);
if($Formval["availability_country"]!='')
	$Formval['availability_country'] = $Formval["availability_country"];
else
	$Formval['availability_country'] = $Formval["country"];

$countrySql = "SELECT country_id,country_name FROM ".TABLEPREFIX."_country ORDER BY country_name";
$CountryArr = $UserManagerObjAjax->HtmlOptionArrayCreate($countrySql);

$Sqlcity = "SELECT * FROM ".TABLEPREFIX."_cities where city_id='".$Formval['city']."'";
$CityArr =  $UserManagerObjAjax->GetRecords("Row",$Sqlcity);
$Formval['city_name'] = $CityArr['city_key'];

$Sqlcity = "SELECT * FROM ".TABLEPREFIX."_cities where city_id='".$Formval['availability_city']."'";
$CityArr =  $UserManagerObjAjax->GetRecords("Row",$Sqlcity);
$Formval['availabilitycity_name'] = $CityArr['city_key'];

$Sqltype = "SELECT * FROM ".TABLEPREFIX."_user_type_user_relation WHERE user_id='".$_SESSION['user_id']."'";
$typeArr = $UserManagerObjAjax->GetRecords("All",$Sqltype);
$Numtype = count($typeArr);
$is_coach = 0;
$is_partner = 0;
for($t=0;$t<$Numtype;$t++)
{
   if($typeArr[$t]['user_type_id']==1)
      $is_coach = 1;
   if($typeArr[$t]['user_type_id']==3)
      $is_partner = 1;
}

$langSql = "SELECT language_id,language_name FROM ".TABLEPREFIX."_language ORDER BY language_name";
$langArr = $UserManagerObjAjax->GetRecords("All",$langSql);
$language_id = explode(',',$Formval['language_ids']);
$language_ids = $language_id;

$smarty->assign('user_type',$_SESSION['user_type']);
$smarty->assign('langArr',$langArr);
$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);
$smarty->assign('language_ids',$language_ids);
$smarty->assign('CountryArr',$CountryArr);
$smarty->assign('Formval',$Formval);
$smarty->display("edit_profile.tpl");
include("footer.php");
?>